package com.link.base.tencent.corpwx.device.service;

import javax.annotation.Resource;

import com.google.gson.JsonObject;
import com.link.base.core.baidu.BaiDuMapUtil;
import com.link.base.user.model.CoreUser;
import com.link.core.basic.query.Filter;
import com.link.core.basic.query.QueryParams;
import com.link.core.cllog.LCLogger;
import com.link.core.util.StringUtils;
import com.link.core.util.UserUtil;
import org.springframework.stereotype.Service;
import com.link.base.tencent.corpwx.device.dao.mybatis.mapper.CorpWxDeviceMapper;
import com.link.base.tencent.corpwx.device.model.CorpWxDevice;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;

import java.util.List;

/**
 * 企业微信设备管理-
 *
 * @author 郭闪
 * @date 2020/06/29 16:21
 */
@Service
public class CorpWxDeviceServiceImpl extends BasicServiceImpl<CorpWxDevice> implements CorpWxDeviceService {

    @Resource
    private CorpWxDeviceMapper    corpWxDeviceMapper;

    @Override
    public BasicMapper<CorpWxDevice> getBasicMapper() {
        return corpWxDeviceMapper;
    }

    @Override
    public void insert(CorpWxDevice entity) throws Exception {
        CoreUser user = UserUtil.getUser();
        entity.setId(keyGenerateService.keyGenerate());
        if (user == null) {
            LCLogger.withException("获取不到用户信息");
            addAddrAndInsert(entity);
        } else {
            entity.setUserId(user.getId());
            QueryParams qps = new QueryParams();
            qps.setPage(1);
            qps.setRows(1);
            qps.addFilter(new Filter("userId",Filter.OP_EQUAL,user.getId().toString()));
            qps.addFilter(new Filter("model",Filter.OP_EQUAL,entity.getModel()));
            qps.addFilter(new Filter("brand",Filter.OP_EQUAL,entity.getBrand()));
            entity.setQps(qps);
            List<CorpWxDevice> corpWxDevices = queryByExamplePage(entity);
            if (corpWxDevices.isEmpty()) {
                addAddrAndInsert(entity);
            } else {
                CorpWxDevice corpWxDevice = corpWxDevices.get(0);
                Long id = corpWxDevice.getId();
                entity.setId(id);
                entity.setProvince(corpWxDevice.getProvince());
                entity.setCity(corpWxDevice.getCity());
                entity.setAddress(corpWxDevice.getAddress());
                corpWxDeviceMapper.update(entity);
            }
        }

    }

    /**
     * 将经纬度转换为地址，并存储
     * @param entity
     * @throws Exception
     */
    private void addAddrAndInsert(CorpWxDevice entity) throws Exception {
        String longitude = entity.getLongitude();
        String latitude = entity.getLatitude();
        if (StringUtils.isNotBlank(longitude) && StringUtils.isNotBlank(latitude)) {
            JsonObject jsonObject = BaiDuMapUtil.reverseGeocoding(Double.valueOf(longitude), Double.valueOf(latitude), null);
            JsonObject asJsonObject = jsonObject.getAsJsonObject("result");
            JsonObject addressComponent = asJsonObject.getAsJsonObject("addressComponent");

            entity.setAddress(asJsonObject.get("formatted_address").getAsString());
            entity.setCity(addressComponent.get("city").getAsString());
            entity.setProvince(addressComponent.get("province").getAsString());
        }
        corpWxDeviceMapper.insert(entity);
    }

}

